package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.CheckGroupMapper;
import com.itcast.dao.SetmealMapper;
import com.itcast.health.constant.RedisConstant;
import com.itcast.health.pojo.CheckGroup;
import com.itcast.health.pojo.PageResult;
import com.itcast.health.pojo.QueryPageBean;
import com.itcast.health.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * @author Gao
 * @version v1.0
 * @date 2019/11/20 10:40
 * @description
 **/
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private JedisPool jedisPool;
    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal>page=setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.add(setmeal);
        //添加关系
        addRelationship(setmeal.getId(),checkgroupIds);

        savePic2Redis(setmeal.getImg());

        Jedis resource = jedisPool.getResource();
        resource.del("AllSetmeal");
        Set<String> keys = resource.keys("*");


    }

    /**
     * 查询编辑套餐时需要回显的数据
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        Map map=new HashMap();
      Setmeal setmeal=  setmealMapper.findSetmealById(id);
      map.put("setmeal",setmeal);
        List<CheckGroup> checkGroups = checkGroupMapper.findAll();
        map.put("checkGroups",checkGroups);
      List <Integer>checkgroupIds= setmealMapper.findRelationship(id);
      map.put("checkgroupIds",checkgroupIds);
        return map;
    }

    /**
     * 编辑套餐
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.update(setmeal);
        savePic2Redis(setmeal.getImg());
        //添加关系
        addRelationship(setmeal.getId(),checkgroupIds);

        Jedis resource = jedisPool.getResource();
        resource.del("AllSetmeal");
        resource.del("Setmeal"+setmeal.getId());

        }


    /**
     * 根据id删除套餐
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //删除套餐前要删除外键中的关联的数据
        setmealMapper.deleteRelationship(id);
        setmealMapper.delete(id);
        Jedis resource = jedisPool.getResource();
        resource.del("AllSetmeal");
        Set<String> keys = resource.keys("*");
        for (String key : keys) {
            if (key.contains("Setmeal")){
                resource.del("Setmeal");
            }
        }
    }

    /**
     * 新建关系的抽取方法
     * @param id
     * @param checkgroupIds
     */
    private void addRelationship(Integer id, Integer[] checkgroupIds) {
        setmealMapper.deleteRelationship(id);
        if (checkgroupIds.length>0){
            List<Map> list=new ArrayList<>();
            for (Integer checkgroupId : checkgroupIds) {
                Map map=new HashMap();
                map.put("setmealId",id);
                map.put("checkgroupId",checkgroupId);
                list.add(map);
            }
            setmealMapper.addRelationship(list);
        }
    }

    private void savePic2Redis(String pic){
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);
    }
}